package com.tg.app.view;

import android.content.res.Resources;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.tg.app.R;
import com.tange.module.device.feature.DeviceFeature;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tg.appcommon.android.ResourcesUtil;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGLog;

import java.util.ArrayList;
import java.util.List;

public class QualityUIHelper {

    private static String getType2Str(DeviceFeature deviceFeature, int type) {
        int size = deviceFeature.getSupportResolutions().size();
        String res = null;
        switch (type) {
            case AVIOCTRLDEFs.AVIOCTRL_QUALITY_MAX:
                res = deviceFeature.getSupportResolutions().get(0);
                break;
            case AVIOCTRLDEFs.AVIOCTRL_QUALITY_UNKNOWN:
            case AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN:
                res = deviceFeature.getSupportResolutions().get(size - 1);
                break;
            case AVIOCTRLDEFs.AVIOCTRL_QUALITY_LOW:
                break;
            case AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIDDLE:
                if (size == 4) {
                    res = deviceFeature.getSupportResolutions().get(2);
                } else {
                    res = deviceFeature.getSupportResolutions().get(size / 2);
                }
                break;
            case AVIOCTRLDEFs.AVIOCTRL_QUALITY_HIGH:
                if (size > 3) {
                    res = deviceFeature.getSupportResolutions().get(1);
                }
                break;
        }
        return res;
    }

    public static int getQualityResId(DeviceFeature deviceFeature, int type) {
        if (deviceFeature == null) return 0;

        if (deviceFeature.getSupportResolutions() != null) {
            String str = getType2Str(deviceFeature, type);
            TGLog.d("CameraBaseActivity", "getQualityResId: " + str);
            if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_SD)) {
                return R.drawable.ic_tange_live_sd;
            } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_HD)) {
                return R.drawable.ic_tange_live_hd;
            } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_FHD)) {
                return R.drawable.ic_tange_live_fhd;
            } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_UHD)){
                return R.drawable.ic_tange_live_uhd;
            } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_FUD)) {
                return R.drawable.ic_tange_live_fhd;
            } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_WQHD)) {
                return R.drawable.ic_tange_live_uhd;
            }

        } else {
            if (type == AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN) {
                return R.drawable.ic_tange_live_sd;
            } else {
                return R.drawable.ic_tange_live_hd;
            }
        }
        return 0;
    }

    public static String getQualityType(DeviceFeature deviceFeature, int type) {
        if (deviceFeature.getSupportResolutions() != null) {
            if (deviceFeature.getSupportResolutions().size() < 2) return null;
            String res = getType2Str(deviceFeature, type);

            if (!TextUtils.isEmpty(res)) {
                return getQualitysStr(res);
            }
        } else {
            Resources resources = ResourcesUtil.getResources();
            if (type == AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN) {
                return resources.getString(R.string.normal_definition);
            } else {
                return resources.getString(R.string.high_definition);
            }
        }
        return "";
    }

    public static int getIndex(DeviceFeature deviceFeature, int type) {
        if (deviceFeature.getSupportResolutions() != null) {
            if (deviceFeature.getSupportResolutions().size() < 2) {
                return 0;
            }
            int size = deviceFeature.getSupportResolutions().size();
            int res = 0;
            switch (type) {
                case AVIOCTRLDEFs.AVIOCTRL_QUALITY_MAX:
                    res = 0;
                    break;
                case AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN:
                    res = size - 1;
                    break;
                case AVIOCTRLDEFs.AVIOCTRL_QUALITY_LOW:
                    break;
                case AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIDDLE:
                    if (size == 4) {
                        res = 2;
                    } else {
                        res = size / 2;
                    }
                    break;
                case AVIOCTRLDEFs.AVIOCTRL_QUALITY_HIGH:
                    if (size > 3) {
                        res = 1;
                    }
                    break;
            }
            return res;
        }
        return  (type == AVIOCTRLDEFs.AVIOCTRL_QUALITY_MIN) ? 1 : 0;
    }

    private static String getQualitysStr(String str) {
        Resources resources = ResourcesUtil.getResources();
        if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_SD)) {
            return resources.getString(R.string.normal_definition);
        } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_HD)) {
            return resources.getString(R.string.high_definition);
        } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_FHD)) {
            return resources.getString(R.string.full_high_definition);
        } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_UHD)){
            return resources.getString(R.string.full_high_definition);
        } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_FUD)) {
            return resources.getString(R.string.full_high_definition);
        } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_WQHD)){
            return resources.getString(R.string.full_high_definition_uhd);
        }
        return null;
    }

    @NonNull
    public static List<String> getQualitys(DeviceFeature deviceFeature) {
        if (deviceFeature.getSupportResolutions() != null) {
            List<String> as = new ArrayList<>();
            Resources resources = ResourcesUtil.getResources();
            for (String str : deviceFeature.getSupportResolutions()) {
                if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_SD)) {
                    as.add(resources.getString(R.string.normal_definition));
                } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_HD)) {
                    as.add(resources.getString(R.string.high_definition));
                } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_FHD)) {
                    as.add(resources.getString(R.string.full_high_definition));
                } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_UHD)){
                    as.add(resources.getString(R.string.full_high_definition));
                } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_FUD)) {
                    as.add(resources.getString(R.string.full_high_definition));
                } else if (StringUtils.equalsIgnoreCase(str, DeviceFeature.QUALITY_WQHD)) {
                    as.add(resources.getString(R.string.full_high_definition_uhd));
                }
            }
            return as;
        }
        return null;
    }
}
